<?php

if(!defined("INDEX"))
    die;

if(!check_post('releasedate', 'trackcount', 'length', 'artistname'))
    throw new HackException();

// ----------------------------------------------------------------------------

isset($_GET['update']) ? $update = true : $update = false;

$dbo = DBO::getInstance();

$dbo->autoCommit(false);

try {

    /* RELEASE */
    $release = new Release;

    if(isset($_POST['release_id'])) {
        $release->id = $_POST['release_id'];
        $release->load();
    }

    

    $release->release_date = $_POST['releasedate'];
    $release->track_count = $_POST['trackcount'];
    $release->length = get_seconds($_POST['length']);
    $release->record_type_id = $_POST['recordtype'];
    if(!empty($_POST['tba'])) {
        $release->tba = ($_POST['tba'] == 'on');
        $release->name = 'To be announced';
    }
    else {
        $release->tba = false;
        $release->name = $_POST['releasename'];
    }

    if($release->name == 'To be announced')
        $release->tba = true;

    if(!empty($_FILES['picture']['name']))
        $release->has_picture = true;
    elseif(!empty($_POST['picture_url']))
        $release->has_picture = true;
    elseif(!empty($_POST['has_picture']))
        $release->has_picture = true;

    $release_id = $release->save();

    /* ARTIST */
    if(!Artist::exists($_POST['artistname'])) {
        $artist = new Artist;
        $artist->name = $_POST['artistname'];

        $artist_id = $artist->save();

        // Linking with lastfm tags
        try {
            $lastfm = new Lastfm;
            $lastfm->updateArtistTags($artist->id);
            $lastfm->updateArtistPicture($artist->id);
        } catch (Exception $e) {
            // TODO: log it
        }

    } else {

        $artist = Artist::getByName($_POST['artistname']);
    }

    $release_year = null;

    if(is_upcoming_date($_POST['releasedate'])) {

        $chk_upcoming_id = $artist->hasUpcomingRelease();

        if(!empty($chk_upcoming_id) && !$update)
            throw new Exception(_('Artist already has upcoming release. You can edit it '). sprintf('<a href="/releases/%u">%s</a>', $chk_upcoming_id, _('here')));

        if(!$update)
            $artist->linkWithRelease($release_id);

    } else {
        
        $data = explode('-', $_POST['releasedate']);

        $release_year = $data[0];

        if(!$update)
            if(!$artist->linkedWithRelease($release->name, $release_year))
                $artist->linkWithRelease($release_id);
    }

    /* LABEL */
    if(!$update) {
        if(strlen(trim($_POST['labelname'])) > 0) {

            if(!Label::exists($_POST['labelname'])) {

                $label = new Label;
                $label->name = $_POST['labelname'];

                $label_id = $label->save();

                $release->linkWithLabel($label_id);

            } else {

                $label = Label::getByName($_POST['labelname']);

                if(!$release->linkedWithLabel($_POST['labelname']))
                    $release->linkWithLabel($label->id);
            }
        }
    }

    // Upload picture
    if(!empty($_FILES['picture']['name'])) {

        $release->uploadPicture($_FILES['picture']);

    } elseif(!empty($_POST['picture_url'])) {

        $release->uploadPictureFromUrl($_POST['picture_url']);
    }

    $dbo->commit();

    $smarty->assign('release_id', $release->id);

} catch(CoreException $e) {

    $dbo->rollback();

    $smarty->assign('errors', array(_('Core exception occured. Sorry!')));

} catch(Exception $e) {

    $dbo->rollback();

    $smarty->assign('errors', array($e->getMessage()));
}